/**
 * create the sky using the big box
 */

sky_vertex = '\
	#ifdef GL_ES \n\
	precision highp float; \n\
	#endif \n\
	attribute vec3 position; \n\
	attribute vec2 texCoord; \n\
	\n\
	varying vec2 vTexCoord; \n\
	\n\
	uniform mat4 view; \n\
	uniform mat4 projection; \n\
	\n\
	void main(void) { \n\
		vec4 worldPosition = view * vec4(position, 1.0); \n\
		vTexCoord = texCoord; \n\
		gl_Position = projection * worldPosition; \n\
	} \n\
	';

sky_fragment = '\
	#ifdef GL_ES \n\
	precision highp float; \n\
	#endif \n\
	varying vec2 vTexCoord; \n\
	uniform sampler2D texture; \n\
	\n\
	void main(void) { \n\
		vec4 diffuseColor = texture2D(texture, vTexCoord); \n\
		gl_FragColor = vec4(diffuseColor.rgb, diffuseColor.a); \n\
	} \n\
	';

var skyVerts  = [  
                 	//-256, 256, -256, 0.251, 0.249,
                 	//256, 256, -256, 0.499, 0.249,
                 	//-256, -256, -256, 0.251, 0.001,
                 	//256, -256, -256, 0.499, 0.001,
                 
                    -512, 512, 512, 0.251, 0.501,
             		512, 512, 512, 0.499, 0.501,
             		-512, -512, 512, 0.251, 0.749,
             		512, -512, 512, 0.499, 0.749,
                  		
                	-512, 512, 512, 0.251, 0.499,
                	512, 512, 512, 0.499, 0.499,
                	-512, 512, -512, 0.251, 0.251,
                	512, 512, -512, 0.499, 0.251,
                		
                	-512, -512, 512, 0.251, 0.751,
                	512, -512, 512, 0.499, 0.751,
                	-512, -512, -512, 0.251, 0.997,
                	512, -512, -512, 0.499, 0.997,
                		
                	512, 512, 512, 0.501, 0.501,
                	512, -512, 512, 0.501, 0.749,
                	512, 512, -512, 0.749, 0.501,
                	512, -512, -512, 0.749, 0.749,
                		
                	-512, 512, 512, 0.249, 0.501,
                	-512, -512, 512, 0.249, 0.749,
                	-512, 512, -512, 0.001, 0.501,
                	-512, -512, -512, 0.001, 0.749,];

var skyIndexs = [	0, 1, 2,
	        		1, 2, 3,
	        		
	        		4, 5, 6,
	        		5, 6, 7,
	        		
	        		8, 9, 10,
	        		9, 10, 11,
	        		
	        		12, 13, 14,
	        		13, 14, 15,
	        		
	        		16, 17, 18,
	        		17, 18, 19,	
	        		
	        	];

var sky_attribs  = [ 'position', 'texCoord' ];
var sky_uniforms = [ 'view', 'projection', 'texture' ];

var sky_url = "textures/sky/skybox.jpg";
var sky_stride = 20;

slBoxSky = function( gl )
{
	this.gl				= gl;
	
	this.skyBuffer	 	= null;
	this.skyIndexBuffer = null;
	this.skyIndexCount	= 0;
	this.skyMatrix		= new slMatrix();
	
	this.skyShader		= null;
};

slBoxSky.prototype.init = function()
{
	if( this.gl == null )
		return false;
	
	// initialize buffer
	this.skyBuffer 		= gl.createBuffer();
	gl.bindBuffer( gl.ARRAY_BUFFER, this.skyBuffer );
	gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( skyVerts ), gl.STATIC_DRAW );
	
	this.skyIndexBuffer = gl.createBuffer();
	gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, this.skyIndexBuffer );
	gl.bufferData( gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( skyIndexs ), gl.STATIC_DRAW );
	
	this.skyIndexCount = skyIndexs.length;
	// create the shader
	this.createSkyShader(sky_vertex, sky_fragment, sky_attribs, sky_uniforms);
	
};

slBoxSky.prototype.createSkyShader = function( vertex, fragment, attribs, uniforms )
{

	var sky_stage = {
			texture   : null,
			program   : null
	};
	
	sky_stage.program = slShader.compileProgram(this.gl, vertex, fragment, attribs, uniforms);
	slShader.loadTextureUrl(this.gl, sky_stage, sky_url, function( texture )
	{
				sky_stage.texture = texture;
	});
	 
	this.skyShader = {
			cull   : gl.FONT,
			blend  : false,
			stages :[ sky_stage ]
		};
};

slBoxSky.prototype.bindAttribs = function( shader ,view, projection )
{	
	if( shader == null )
		return false;
	var gl = this.gl;
	
	slMatrixSet( this.skyMatrix, view );
	
	this.skyMatrix.m[12] = 0;
	this.skyMatrix.m[13] = 0;
	this.skyMatrix.m[14] = 0;
	
	gl.uniformMatrix4fv( shader.uniform.view, false, slMatrixAsFloat32Array( this.skyMatrix ) );
	gl.uniformMatrix4fv( shader.uniform.projection, false, slMatrixAsFloat32Array( projection ));

	gl.enableVertexAttribArray( shader.attrib.position );
	gl.vertexAttribPointer(  shader.attrib.position, 3, gl.FLOAT, false, sky_stride, 0 );
	
	if( shader.attrib.texCoord != -1 )
	{
		gl.enableVertexAttribArray( shader.attrib.texCoord );
		gl.vertexAttribPointer( shader.attrib.texCoord, 2, gl.FLOAT, false, sky_stride, 3*4 );
	}
};

slBoxSky.prototype.draw = function( context )
{
	if( this.skyBuffer == null || this.skyIndexBuffer == null )
		return;
	
	var time = context.elapsedTime / 1000;
	var view_mat = context.view;
	var projection_mat = context.projection;
	
	if(this.skyShader) {
		
		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.skyIndexBuffer);
		gl.bindBuffer(gl.ARRAY_BUFFER, this.skyBuffer);
	
		if(slShader.setShader(gl, this.skyShader)) { 
			for(var j = 0; j < this.skyShader.stages.length; ++j) {
				var stage = this.skyShader.stages[j];
				
				var shaderProgram = slShader.setShaderStage(gl, this.skyShader, stage, time );
				if(!shaderProgram) { continue; }
				this.bindAttribs( shaderProgram, view_mat, projection_mat );
				
				gl.drawElements(gl.TRIANGLES, this.skyIndexCount, gl.UNSIGNED_SHORT, 0);
			}
		}
	}
	
};